package com.heima.wemedia.service.impl;

import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.NewsPageDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.vo.StatisticsVo;
import com.heima.wemedia.mapper.ReportServiceMapper;
import com.heima.wemedia.service.ReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportServiceMapper reportServiceMapper;

    /**
     * 图文统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ResponseResult viewReport(LocalDate begin, LocalDate end) {
        //1.找到每一天的日期
        ArrayList<LocalDate> list = new ArrayList<>();
        list.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        //点赞数量
        ArrayList<Integer> likeList = new ArrayList<>();
        //收藏数量
        ArrayList<Integer> collectList = new ArrayList<>();
        //文章数量
        ArrayList<Integer> numList = new ArrayList<>();
        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //处理每天的点赞数
            Integer allLikes = reportServiceMapper.countLikes(beginTime, endTime);
            //处理每天的收藏数量
            Integer allCollect = reportServiceMapper.countCollect(beginTime, endTime);
            //统计出这段时间发布了多少文章
            Integer allpublishNum = reportServiceMapper.countpublishNum(beginTime,endTime);
            if (allCollect != null) {
                collectList.add(allCollect);
            }
            if (allLikes != null) {
                likeList.add(allLikes);
            }
            if (allpublishNum != null){
                numList.add(allpublishNum);
            }

        }

        Integer likeCount = 0;
        for (Integer i : likeList) {
            likeCount = i + likeCount;
        }
        Integer collectCount = 0;
        for (Integer i : collectList) {
            collectCount = collectCount + i;
        }
        Integer publishNum = 0;
        for (Integer i : numList) {
            publishNum = publishNum + i;
        }

        //返回值
        return ResponseResult.okResult(StatisticsVo
                .builder()
                .likesNum(likeCount)
                .collectNum(collectCount)
                .publishNum(publishNum)
                .build());
    }
}
